package edu.unu.iist.ts2es.format;

import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Graphml2Dot {

	public static String LineBreak = "\r\n";

	public static void transform(String src, String des) throws IOException,
			ParserConfigurationException, SAXException {
		File srcFile = new File(src);
		if (!srcFile.exists()) {
			System.out.println("File not found:" + srcFile.getAbsolutePath());
			return;
		}

		File desFile = new File(des);
		if (desFile.exists()) {
			desFile.delete();
		}

		FileWriter writer = new FileWriter(desFile);

		desFile.createNewFile();

		writer.write("digraph G {");
		writer.write(LineBreak);

		// GraphML<String, String> graphml = GraphML.getGraphML(src);
		/*
		 * Map<String, GraphMLMetadata<String>> vertexs =
		 * graphml.getVertexData(); for(String key: vertexs.keySet()) {
		 * 
		 * }
		 */

		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(srcFile);
		doc.getDocumentElement().normalize();
		NodeList nList = doc.getElementsByTagName("edge");
		for (int i = 0; i < nList.getLength(); i++) {
			Node node = nList.item(i);
			NamedNodeMap attrs = node.getAttributes();
			String from = attrs.getNamedItem("source").getNodeValue();
			String to = attrs.getNamedItem("target").getNodeValue();
			writer.write(from + "->" + to + " ");
			if (node.hasChildNodes()) {
				NodeList nodes2 = node.getChildNodes();
				Node node1 = nodes2.item(1);
				String label = node1.getFirstChild().getNodeValue();
				writer.write("[label=\"" + label + "\"];");
			}
			writer.write(LineBreak);
			// NamedNodeMap attrs2 = attrs;
			// writer.write();
		}

		/*
		 * Map<String, GraphMLMetadata<String>> edges = graphml.getEdgeData();
		 * DirectedSparseGraph<String, String> graph = graphml.getGraph();
		 * for(String edge: graph.getEdges()){
		 * writer.write(graph.getSource(edge)); writer.write("->");
		 * writer.write(graph.getDest(edge)); String label =
		 * edges.get(edge).transformer.transform(edge); writer.write("[label=\""
		 * +label + "\"];"); writer.write(LineBreak); }
		 */
		writer.write("}");
		writer.close();
	}

	public static void main(String[] args) {

		String src = "D:/testcases/graphml/";
		File folder = new File(src);
		if (folder.exists() && folder.isDirectory()) {

			File[] lists = folder.listFiles(new FilenameFilter() {
				@Override
				public boolean accept(File dir, String name) {
					String suffix = name.substring(name.lastIndexOf(".")+1,
							name.length());
					if (suffix.equalsIgnoreCase("graphml")) {
						return true;
					}
					return false;
				}
			});

			for (File file : lists) {
				String filename = file.getName();
				String filename2 = filename.substring(0,
						filename.lastIndexOf("."));
				String des = file.getParent()+"\\"+ filename2 + "_subev" + ".dot";
				try {
					transform(file.getAbsolutePath(), des);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
		/*
		 * try { transform(src,des); } catch (IOException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); } catch
		 * (ParserConfigurationException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } catch (SAXException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); }
		 */
	}

}
